﻿Imports MeoTom.Core.BLL
Imports MeoTom.Core

Namespace Control

    Partial Public Class ArticleCategory1
        Inherits UserControl
        Private ReadOnly _cat As New Categories()
        Private ReadOnly _util As New Utilities.Common()

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
            Dim previousPage As String = ""
            If Request.QueryString.ToString = vbNullString Then
                previousPage = Server.UrlEncode(Request.ServerVariables("PATH_INFO"))
            Else
                previousPage = Server.UrlEncode(Request.ServerVariables("PATH_INFO") & "?" & Request.QueryString.ToString())
            End If
            _util.ValidateLogin(previousPage)

            LoadCategory()
        End Sub

        Public Sub LoadCategory()
            CatDetail.InnerHtml = _cat.WriteCategoryInside(1)
            cboCategory.InnerHtml = _cat.WriteCategoryInside(2)
        End Sub

        Private Sub btnCreate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCreate.Click
            Try
                If txtTitle.Text = vbNullString Then
                    lblError.Attributes.Remove("style")
                    lblError.InnerHtml = "* Category Name Require !!"
                    txtTitle.Focus()
                Else
                    If txtTitle.Text.Length > 50 Then
                        lblError.Attributes.Remove("style")
                        lblError.InnerHtml = "* Category Name is too long !!"
                        txtTitle.Focus()
                    Else
                        If txtDesc.Text.Length > 450 Then
                            lblError.Attributes.Remove("style")
                            lblError.InnerHtml = "* Description is too long !!"
                            txtDesc.Focus()
                        Else
                            If Not _util.IsNumeric(txtCatIndex.Text) Then
                                lblError.Attributes.Remove("style")
                                lblError.InnerHtml = "* Category Index must be number!!"
                                txtDesc.Focus()
                            Else
                                _cat.Title = txtTitle.Text
                                _cat.Description = txtDesc.Text
                                _cat.CatIndex = Integer.Parse(txtCatIndex.Text)
                                _cat.SubCat = Integer.Parse(Request.Form("cboCategory").ToString())
                                _cat.Create()
                                _cat.WriteCategory()
                                txtTitle.Enabled = False
                                txtCatIndex.Enabled = False
                                txtDesc.Enabled = False
                                btnCreate.Enabled = False
                                btnApply.Enabled = False
                                btnApply2.Enabled = False
                                CatDetail.InnerHtml = _cat.WriteCategoryInside(1)
                                cboCategory.InnerHtml = _cat.WriteCategoryInside(2)
                                txtTitle.Enabled = True
                                txtCatIndex.Enabled = True
                                txtDesc.Enabled = True
                                txtCatIndex.Text = "0"
                                txtDesc.Text = ""
                                txtTitle.Text = ""
                                btnCreate.Enabled = True
                                btnApply.Enabled = True
                                btnApply2.Enabled = True
                            End If
                        End If
                    End If
                End If
            Catch ex As Exception
            End Try
        End Sub

        Protected Sub btnApply_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnApply.Click
            Try
                Const i As Integer = 0
                If Request.Form("selectedAction0") = "1" Or Request.Form("selectedAction1") = "1" Then
                    'Edit Category
                    Dim vId As String = ""
                    For Each str As String In Regex.Split(Request.Form("cboCheck"), ",")
                        If i = 0 Then
                            vId = str
                            If Not IsNumeric(vId) Then
                                vId = "1"
                            End If
                            Exit For
                        End If
                    Next
                    Response.Redirect("/mt-inside/ArticleCategory.aspx?action=Edit&CatID=" & vId, False)
                ElseIf Request.Form("selectedAction0") = "2" Or Request.Form("selectedAction1") = "2" Then
                    'Delete
                    For Each str As String In Regex.Split(Request.Form("cboCheck"), ",")
                        DeleteCat(Integer.Parse(str))
                    Next
                    _cat.WriteCategory()
                    Response.Redirect("/mt-inside/ArticleCategory.aspx", False)
                End If
            Catch ex As Exception
            End Try
        End Sub

        Private Sub btnApply2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnApply2.Click
            btnApply_Click(sender, e)
        End Sub

        Public Sub DeleteCat(ByVal vId As Integer)
            _cat.CatID = vId
            _cat.Delete()
            _cat.WriteCategory()
        End Sub
    End Class
End Namespace